﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;

namespace DvdDecrypterHelper
{
    class Program
    {
        private static string GENERAL_DefaultDestinationFolder = "";
        //
        static void Main(string[] args)
        {
            GENERAL_DefaultDestinationFolder = getDestinationDirectory();
            //
            if(""==GENERAL_DefaultDestinationFolder)
            {
                Console.WriteLine("ERRROR: No GENERAL_DefaultDestinationFolder in registry!");
                Console.ReadKey();
                return;
            }
            //
            //
            Console.WriteLine("GENERAL_DefaultDestinationFolder: [" + GENERAL_DefaultDestinationFolder + "]");
            //
            FileSystemWatcher watcher = new FileSystemWatcher(GENERAL_DefaultDestinationFolder, "VTS*.*");
            watcher.Created += new FileSystemEventHandler(OnCreated);
            watcher.Changed += new FileSystemEventHandler(OnChanged);

            // Begin watching.
            watcher.EnableRaisingEvents = true;
            Console.ReadKey();
//File: d:\Vob-0\DVD Decrypter\VTS_01_0.IFO Changed
//File: d:\Vob-0\DVD Decrypter\VTS_01_0.IFO Changed
//File: d:\Vob-0\DVD Decrypter\VTS_01_0.IFO Changed

//File: d:\Vob-0\DVD Decrypter\VTS_01_1.VOB Created
//File: d:\Vob-0\DVD Decrypter\VTS_01_1.VOB Changed
        }


        static string vobName = "";
        private static void OnCreated(object source, FileSystemEventArgs e)
        {
            if (Path.GetExtension(e.Name).ToUpper() == ".VOB")
            {
                vobName= e.FullPath;
            }
        }

        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or deleted.
            if(Path.GetExtension(e.Name).ToUpper() == ".IFO")
            {
                if(File.Exists(e.FullPath))
                {
                    try
                    {
                        File.Delete(e.FullPath);
                        Console.WriteLine("Delete: "+e.Name);
                    }
                    catch { }
                }
            }
            else if (Path.GetExtension(e.Name).ToUpper() == ".VOB")
            {
                if(vobName == e.FullPath)
                {
                    int n = GetLastPageNumber(Path.GetDirectoryName(e.FullPath)) + 1;
                    File.Move(e.FullPath,Path.Combine(Path.GetDirectoryName(e.FullPath),string.Format("{0:D2}.vob", n)));
                    Console.WriteLine("Rename: " + e.Name);
                }
            }
        }

        public static int GetLastPageNumber(string DestiantionDirectory)
        {
            int n = -1;
            //
            try
            {
                string[] ff = Directory.GetFiles(DestiantionDirectory, "??.VOB");
                if(0==ff.Count())
                {
                    return 0;
                }
                //
                foreach (string s in ff)
                {
                    string t = Regex.Replace(System.IO.Path.GetFileName(s), "[^0-9]", "");
                    int m = 0;
                    if (int.TryParse(t, out m))
                    {
                        if (m > n) n = m;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("ERRROR: " + e.Message + "  " + e.Source);
                //MessageBox.Show(e.Message, e.Source);
            }
            return n;
        }

        private static string getDestinationDirectory()
        {
            string s = "";
            //HKEY_CURRENT_USER\Software\DVD Decrypter
            //GENERAL_DefaultDestinationFolder
            try
            {
                var r = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\DVD Decrypter", false);
                s = (string)r.GetValue(@"GENERAL_DefaultDestinationFolder", "");
            }
            catch
            {

            }
            return  Path.GetDirectoryName(s);
        }
    }//class
}//namespace
